package com.zhanglijie.theory.ArrayListDemo.cponwriteArraylist;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/7/25 0025 22:30
 */
public class WriteThread  extends Thread {
    private CopyOnWriteArrayList<Integer> cpList;
    public WriteThread(CopyOnWriteArrayList<Integer> cp){
        this.cpList = cp;
    }
    @Override
    public void run() {
        Iterator<Integer> iterator = cpList.iterator();//尽管包装了但是迭代器不加锁。所以需要手动加上锁
        while (true) {
            if(!iterator.hasNext()){
                iterator = cpList.iterator();
            }
            Integer next = iterator.next();
            System.out.println(next);
        }
    }
}
